package com.zzmcc.sys.controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.zzmcc.base.BaseController;
import com.zzmcc.common.AuthorizeInterceptor;
import com.zzmcc.sys.model.Authorize;

import util.ChristStringUtil;
import util.CommonUtil;
import util.ModelUtils;

import java.util.ArrayList;
import java.util.List;

@Before(AuthorizeInterceptor.class)
public class AuthorizeController extends BaseController {
    public void index(){
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		String tag = getPara("tag");
		if(ChristStringUtil.isNotEmpty(tag)){
			whee.append(" and actionKey like ?");
			param.add("%" + tag + "%");
		}

		Page<Authorize> pager = Authorize.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
				"select * ",
				" from sys_authorize where 1=1 " + whee.toString(),
				param.toArray());

		setAttr("pager",pager);

		setAttr("pager",pager);
		render("../authorize_list.html");
	}
	
	public void add(){
		render("../authorize_input.html");
	}
	

	
	public void save(){
		Authorize model = getModel(Authorize.class, "Authorize");

		ModelUtils.fillForSave(model);
		model.save();
		renderDWZSuccessJson("创建成功！");
	}
	
	public void edit(){
		setAttr("Authorize",Authorize.dao.findById(getPara("id")));
		render("../authorize_input.html");
	}
	
	public void update(){
		String id = getPara("id");
		Authorize model = getModel(Authorize.class, "Authorize");
		model.set("id", id);
		StringBuffer tag = new StringBuffer();
		tag.append(";").append(model.getStr("username")).append(",");
		String realName = model.get("realname");
		if(ChristStringUtil.isNotEmpty(realName)){
			tag.append(";").append(realName).append(",");
		}
		model.set("tag",tag.toString());
		ModelUtils.fillForUpdate(model);
		model.update();
		renderDWZSuccessJson("修改用户成功！");
	}
	
	public void delete(){
		String[] ids = getParaValues("ids");
		StringBuffer noDeleteBuffer = new StringBuffer();
		int noDeleteCount = 0;
		int deleteCount = 0;
		for(String id:ids){
			Authorize m = Authorize.dao.findById(id);

			m.delete();
			deleteCount++;

		}
		renderDWZSuccessJson("操作成功，删除成功"+deleteCount+"个，失败"+noDeleteCount+"个，删除失败的用户名："+noDeleteBuffer.toString());
	}
	

	
}
